﻿<UserControl x:Class="EtlViewer.Viewer.Controls.Merlin"
             x:ClassModifier="internal"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:ctrls="clr-namespace:EtlViewer.Viewer.Controls"
             xmlns:utils="clr-namespace:EtlViewer.Viewer.UIUtils"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             >
    <UserControl.Resources>
        <utils:ParameterOptionConverter x:Key="ParameterOptionConverter" />
        <DataTemplate x:Key="selectorTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="240"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"  Text="{Binding Name}" ></TextBlock>
                <ComboBox Grid.Column="1"
                          HorizontalAlignment="Stretch"
                          IsEditable="True" 
                          ItemsSource="{Binding Options}" 
                          IsTextSearchEnabled="True" 
                          IsTextSearchCaseSensitive="False"
                          DisplayMemberPath="Name"
                          SelectedValuePath="Value"
                          SelectedValue="{Binding ParameterValue, Mode=TwoWay, Converter={StaticResource ParameterOptionConverter}}"
                          ></ComboBox>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="inputTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="240"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Name}" Width="100"></TextBlock>
                <TextBox Grid.Column="1" Text="{Binding ParameterValue, Mode=TwoWay}" 
                         HorizontalContentAlignment="Stretch" IsTabStop="True"></TextBox>
            </Grid>
        </DataTemplate>
        <ctrls:ParameterTemplateSelector StringTemplate="{StaticResource inputTemplate}" 
                                         SelectorTemplate="{StaticResource selectorTemplate}" 
                                         x:Key="ParameterTemplateSelector"></ctrls:ParameterTemplateSelector>

        <Style TargetType="{x:Type ListView}">
            <Setter Property="ItemTemplateSelector" Value="{StaticResource ParameterTemplateSelector}"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Name="bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}" Margin="{TemplateBinding Margin}">
                            <ScrollViewer Margin="{TemplateBinding Padding}">
                                <WrapPanel ItemWidth="350" IsItemsHost="True" 
                                           MinWidth="100"
                                    Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}">
                                </WrapPanel>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Border BorderBrush="{StaticResource GrayBrush8}" BorderThickness="0,0,0,2">
        <Grid Margin="10" >
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <ListView   Grid.Row="0"  Background="Transparent"
                          ItemTemplateSelector="{StaticResource ParameterTemplateSelector}" 
                          ItemsSource="{Binding Parameters}" 
                          HorizontalAlignment="Stretch"  
                          HorizontalContentAlignment="Left"
                          BorderThickness="0"  KeyboardNavigation.TabNavigation="Continue">
            </ListView>
            <Button Grid.Column="1"  
                Command="{Binding BuildCommand}" 
                Padding="10,10,10,10"  
                Margin="10,0,0,0"
                HorizontalAlignment="Left"  
                HorizontalContentAlignment="Stretch"
                 VerticalAlignment="Top">BUILD</Button>
            <Button Grid.Column="2" Name="ClosePanel"
                VerticalAlignment="Top" 
                HorizontalAlignment="Right"
                Click="ClosePanel_Click_1"
                Style="{StaticResource ControlCommandButton}"
                >r</Button>
        </Grid>
    </Border>
</UserControl>
